<!--
 * @FileDescription 坐标系标定表单
 * @Author 杨宇翔
 * @Date 20220627 11:40:31
 * @LastEditors 杨宇翔
 * @LastEditTime 20220627 11:40:31
-->
<template>
    <div class="calibration-attitude-form" v-show="CalibrationComponent.Default.Id.value == 2">
        <div class="settings">
            <i></i>
            <button class="reset" type="button" @click="OnChanged">
                <img :src="state ? `/static/Lidar/ic_zhankai.png` : `/static/Lidar/ic_shouqi.png`">
                <span>标定面板</span>
            </button>
            <div class="setting-item">
                <label for="姿态标定航向角">
                    <span>航向角</span>
                </label>
                <span @mousedown="OnYawAsync(-1)" @mouseup="OnYawAsync(0)"></span>
                <input type="number" name="姿态标定" id="姿态标定航向角" v-model.lazy.number="yaw" step="0.01"
                    @change="OnAngleChangedAsync">
                <span @mousedown="OnYawAsync(1)" @mouseup="OnYawAsync(0)"></span>
            </div>
            <div class="setting-item">
                <label for="姿态标定俯仰角">
                    <span>俯仰角</span>
                </label>
                <span @mousedown="OnPitchAsync(-1)" @mouseup="OnPitchAsync(0)"></span>
                <input type="number" name="姿态标定" id="姿态标定俯仰角" v-model.lazy.number="pitch" step="0.01"
                    @change="OnAngleChangedAsync">
                <span @mousedown="OnPitchAsync(-1)" @mouseup="OnPitchAsync(0)"></span>
            </div>
            <div class="setting-item">
                <label for="姿态标定横滚角">
                    <span>横滚角</span>
                </label>
                <span @mousedown="OnRollAsync(-1)" @mouseup="OnRollAsync(0)"></span>
                <input type="number" name="姿态标定" id="姿态标定横滚角" v-model.lazy.number="roll" step="0.01"
                    @change="OnAngleChangedAsync">
                <span @mousedown="OnRollAsync(-1)" @mouseup="OnRollAsync(0)"></span>
            </div>

            <div class="distance-info">
                <span>距离</span>
                <span>{{ CalibrationComponent.Default.Config.value.Records.get('Distance')! * 1000 | 0 }}mm</span>
            </div>

            <button class="reset" type="button"
                @click="CalibrationComponent.Default.SubmitAttitudeAsync(yaw, pitch, roll)">
                <span>标定</span>
            </button>
        </div>
        <div class="attitude-result" v-show="CalibrationComponent.Default.AttitudeState.value">
            <div class="result">
                <div class="header">
                    <span>标定评价</span>
                    <span class="material-icons"
                        @click="CalibrationComponent.Default.AttitudeState.value = false">×</span>
                </div>
                <div class="content">
                    <span>
                        {{ CalibrationComponent.Default.AttitudePercent.value > 80 ? '恭喜您，标定完成！' : '很遗憾，标定不合格！' }}
                    </span>
                    <span>本次标定准确率{{ CalibrationComponent.Default.AttitudePercent }}%</span>
                </div>
                <div class="button-container">
                    <button class="reset" type="button" @click="CalibrationComponent.Default.ConfirmAttitudeAsync()">
                        <span>确定</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import TimerComponent from '../../../runtime/module/Timer/TimerComponent';
import { Ref, ref } from 'vue';
import { CalibrationComponent } from './LidarComponent';

const state = ref(true);

const sidebarTranslateX = ref('translateX(0)');
const buttonTranslateX = ref('translateX(0)');

function OnChanged() {
    state.value = !state.value;
    sidebarTranslateX.value = state.value ? 'translateX(0)' : 'translateX(100%)';
    buttonTranslateX.value = state.value ? 'translateX(0)' : 'translateX(-6rem)';
}

const yaw: Ref<number> = ref(0);
const pitch: Ref<number> = ref(0);
const roll: Ref<number> = ref(0);

const yawDirection = ref(0);
const pitchDirection = ref(0);
const rollDirection = ref(0);

async function OnAngleChangedAsync(event: Event): Promise<void> {
    const element = event.target as HTMLInputElement;
    let value = element.value != '' ? Number.parseFloat(element.value) : 0;
    value = Math.clamp(value, -5, 5);
    element.value = value.toFixed(2);

    yaw.value = Number.parseFloat(Math.clamp(yaw.value, -5, 5).toFixed(2));
    pitch.value = Number.parseFloat(Math.clamp(pitch.value, -5, 5).toFixed(2));
    roll.value = Number.parseFloat(Math.clamp(roll.value, -5, 5).toFixed(2));

    await CalibrationComponent.Default.OnAttitudeAnglesUpdateAsync(yaw.value, pitch.value, roll.value);
}

async function OnYawAsync(direction: number): Promise<void> {
    yawDirection.value = direction;
    while (yawDirection.value != 0 && yaw.value > -5 && yaw.value < 5) {
        yaw.value = Number.parseFloat((yaw.value + 0.01 * yawDirection.value).toFixed(2));

        await CalibrationComponent.Default.OnAttitudeAnglesUpdateAsync(yaw.value, pitch.value, roll.value);

        await TimerComponent.Default.WaitAsync(100);
    }
}

async function OnPitchAsync(direction: number): Promise<void> {
    pitchDirection.value = direction;
    while (pitchDirection.value != 0 && pitch.value > -5 && pitch.value < 5) {
        pitch.value = Number.parseFloat((pitch.value + 0.01 * pitchDirection.value).toFixed(2));

        await CalibrationComponent.Default.OnAttitudeAnglesUpdateAsync(yaw.value, pitch.value, roll.value);

        await TimerComponent.Default.WaitAsync(100);
    }
}

async function OnRollAsync(direction: number): Promise<void> {
    rollDirection.value = direction;
    while (rollDirection.value != 0 && roll.value > -5 && roll.value < 5) {
        roll.value = Number.parseFloat((roll.value + 0.01 * rollDirection.value).toFixed(2));

        await CalibrationComponent.Default.OnAttitudeAnglesUpdateAsync(yaw.value, pitch.value, roll.value);

        await TimerComponent.Default.WaitAsync(100);
    }
}
</script>

<style scoped>
.calibration-attitude-form {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

    pointer-events: none;
}

.settings {
    position: absolute;
    right: 0;
    top: 34rem;

    display: flex;
    flex-direction: column;
    justify-content: stretch;
    gap: 2rem;

    padding-top: 7rem;
    padding-bottom: 3rem;

    background-color: #131620;

    border-radius: 2rem 0 0 2rem;

    transform: v-bind(sidebarTranslateX);

    transition: transform 300ms;

    pointer-events: initial;
}

.setting-item {
    width: 100%;
    height: 3.6rem;

    display: flex;
    justify-content: flex-start;
    align-items: center;

    padding: 0 2rem 2rem 2rem;
}

.setting-item>input {
    width: 12rem;
    height: 3.6rem;

    appearance: none;
    outline: none;
    background: none;

    padding: 1rem;

    color: #B1B1D4;
    font-size: 1.8rem;
    text-align: center;

    border: 0.1rem solid #36384B;
}

.setting-item>input::-webkit-inner-spin-button,
.setting-item>input::-webkit-outer-spin-button {
    display: none;
}

.setting-item>label {
    width: 6rem;
    margin-right: 2rem;
}

.setting-item>label>span {
    color: #81859C;
    font-size: 1.8rem;
}

.setting-item>span {
    display: inline-block;
    width: 3.6rem;
    height: 3.6rem;
}

.setting-item>span:first-of-type {
    background-image: url('/static/Lidar/ic_left_normal.png');
}

.setting-item>span:first-of-type:hover {
    background-image: url('/static/Lidar/ic_left_select.png');
}

.setting-item>span:last-of-type {
    background-image: url('/static/Lidar/ic_right_normal.png');
}

.setting-item>span:last-of-type:hover {
    background-image: url('/static/Lidar/ic_right_select.png');
}

.distance-info {
    padding-left: 2rem;

    color: #81859C;
    font-size: 1.8rem;
    font-weight: bold;
}

.distance-info>span:last-of-type {
    padding-left: 8rem;
}

.settings>button:first-of-type {
    position: absolute;
    top: 0;

    width: 100%;
    height: 5rem;

    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 1.4rem;

    padding-left: 2.4rem;

    background-color: #131620;

    border-radius: 2.5rem 0 0 2.5rem;
    box-shadow: inset 0 -0.1rem 0 0 #1F1F2D;

    transform: v-bind(buttonTranslateX);

    transition: transform 300ms;
}

.settings>button:first-of-type>span:first-of-type {
    color: #B1B1D4;
    font-size: 1.8rem;
    font-weight: 500;
}

.settings>button:last-of-type {
    width: 10rem;
    height: 4rem;

    margin: auto;

    border-radius: 0.6rem;
    background: linear-gradient(180deg, #807DE1 0%, #218CDE 100%);
}

.settings>button:last-of-type>span {
    color: white;
    font-size: 1.8rem;
}

.settings>i {
    position: absolute;
    left: 0;
    bottom: 0;

    display: block;
    width: 100%;
    height: calc(100% - 6rem);

    border-left: solid 0.6rem transparent;
    border-bottom: solid 0.6rem transparent;
    border-radius: 0 0 0 2rem;
    background-image: linear-gradient(#131620, #131620),
        linear-gradient(90deg, #218cde, #807de1);
    background-origin: border-box;
    background-clip: content-box, border-box;

    z-index: -1;
}

.attitude-result {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

    backdrop-filter: blur(1rem);

    pointer-events: initial;
}

.result {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    width: 64rem;
    height: 38rem;

    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;

    gap: 4rem;

    border-radius: 2rem;
    border: solid 0.6rem transparent;
    background-image: linear-gradient(#131620, #131620),
        linear-gradient(90deg, #218cde, #807de1);
    background-origin: border-box;
    background-clip: content-box, border-box;
}


.result>.header {
    width: 100%;

    padding: 2rem 2rem 0 2rem;

    display: flex;
    justify-content: space-between;
    align-items: center;
}

.result>.header>span:first-of-type {
    color: white;
    font-size: 2.4rem;
    font-weight: 500;
}

.result>.header>.material-icons {
    color: #81859C;
    font-size: 3.6rem;
}

.result>.content {
    flex-grow: 1;

    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;

    gap: 2rem;
}

.result>.content>span:first-of-type {
    color: #B1B1D4;
    font-size: 3rem;
    font-weight: bold;
}

.result>.content>span:last-of-type {
    color: #81859C;
    font-size: 2.4rem;
}

.result>.button-container {
    width: 100%;

    padding-bottom: 4rem;

    display: flex;
    justify-content: space-evenly;
}

.result>.button-container>button {
    width: 25rem;
    height: 10rem;

    background-image: url('/static/Lidar/btn-bg.png');
    background-repeat: no-repeat;
    background-position-y: -0.5rem;
}

.result>.button-container>button>span {
    display: inline-block;

    color: white;
    font-size: 3rem;

    text-indent: 1em;
    letter-spacing: 1em;
}
</style>./LidarComponent